import json
import logging

pro_info = {}

# 如果需要将数据保存至文件里面，则导入json包并解锁以下注释代码和底部的save_to_file()：
# def save_to_file():
#     with open('products.json', 'w') as f:
#         json.dump(pro_info, f)
#
# def load_from_file():
#     try:
#         with open('products.json', 'r') as f:
#             return json.load(f)
#     except FileNotFoundError:
#         return {}


#我不采用存入文档的方式保存数据，采用将数据保存至Linux数据库的方式


def add_product(pro_name, pro_price, pro_stock):
    if pro_name in pro_info:
        return False, "商品已存在"
    pro_info[pro_name] = {"pro_price": pro_price, "pro_stock": pro_stock}
    return True, "商品添加成功"

def view_product(pro_name):
    if pro_name in pro_info:
        return True, f"商品 {pro_name} 的信息：价格 {pro_info[pro_name]['pro_price']}，库存 {pro_info[pro_name]['pro_stock']}"
    else:
        return False, "商品不存在！"

def delete_product(pro_name):
    if pro_name in pro_info:
        del pro_info[pro_name]
        return True, f"商品 {pro_name} 已被删除！"
    else:
        return False, "商品不存在！"

def modify_product(pro_name, pro_price, pro_stock):
    if pro_name in pro_info:
        pro_info[pro_name]["pro_price"] = pro_price
        pro_info[pro_name]["pro_stock"] = pro_stock
        return True, f"商品 {pro_name} 的信息已更新！"
    else:
        return False, "商品不存在！"

def exit_system():
    print("退出系统")
    return True

def validate_input(value):
    try:
        float(value)
        return True
    except ValueError:
        return False

def log_action(action, message):
    logging.info(f"{action}: {message}")

logging.basicConfig(filename='system.log', level=logging.INFO)

pro_info = load_from_file()

while 1:
    choice = input("请输入你的选择: ")

    if choice == "5":
        exit_system()
        break

    # 1. 商品添加
    if choice == "1":
        pro_name = input("请输入商品的名字: ")
        pro_price = input("请输入商品的价格: ")
        pro_stock = input("请输入商品的库存: ")
        while not validate_input(pro_price) or not validate_input(pro_stock):
            print("价格或库存输入无效，请重新输入！")
            pro_price = input("请输入商品的价格: ")
            pro_stock = input("请输入商品的库存: ")
        status, message = add_product(pro_name, pro_price, pro_stock)
        if not status:
            print(message)
        else:
            log_action("添加", message)

    # 2. 查看商品
    elif choice == "2":
        pro_name = input("请输入要查看的商品名字: ")
        status, message = view_product(pro_name)
        if status:
            print(message)
        else:
            print(message)

    # 3. 删除商品
    elif choice == "3":
        pro_name = input("请输入要删除的商品名字: ")
        status, message = delete_product(pro_name)
        if status:
            print(message)
        else:
            print(message)

    # 4. 修改商品
    elif choice == "4":
        pro_name = input("请输入要修改的商品名字: ")
        pro_price = input("请输入新的价格: ")
        pro_stock = input("请输入新的库存: ")
        while not validate_input(pro_price) or not validate_input(pro_stock):
            print("价格或库存输入无效，请重新输入！")
            pro_price = input("请输入新的价格: ")
            pro_stock = input("请输入新的库存: ")
        status, message = modify_product(pro_name, pro_price, pro_stock)
        if status:
            print(message)
        else:
            print(message)

    else:
        print("无效的选择，请重新输入！")

# save_to_file()